package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.PaymentBill;
import cn.wolfcode.crm.domain.PaymentResult;
import cn.wolfcode.crm.domain.SaleAccount;
import cn.wolfcode.crm.mapper.GuaranteebillMapper;
import cn.wolfcode.crm.mapper.PaymentBillMapper;
import cn.wolfcode.crm.mapper.PaymentResultMapper;
import cn.wolfcode.crm.mapper.SaleAccountMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.PaymentBillQueryObject;
import cn.wolfcode.crm.service.IPaymentBillService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class PaymentBillServiceImpl implements IPaymentBillService {

    @Autowired
    private PaymentBillMapper paymentBillMapper;
    @Autowired
    private PaymentResultMapper paymentResultMapper;
    @Autowired
    private GuaranteebillMapper guaranteebillMapper;
    @Autowired
    private SaleAccountMapper saleAccountMapper;

    public int deleteByPrimaryKey(Long id) {
        return paymentBillMapper.deleteByPrimaryKey(id);
    }

    public int insert(PaymentBill record) {
        return paymentBillMapper.insert(record);
    }

    public PaymentBill selectByPrimaryKey(Long id) {
        return paymentBillMapper.selectByPrimaryKey(id);
    }

    public List<PaymentBill> selectAll() {
        return paymentBillMapper.selectAll();
    }

    public int updateByPrimaryKey(PaymentBill record) {
        return paymentBillMapper.updateByPrimaryKey(record);
    }

    public PageResult query(PaymentBillQueryObject qo) {
        int total = paymentBillMapper.queryForCount(qo);
        System.out.println(total);
        if (total == 0) {
            return new PageResult();
        }
        List<PaymentBill> rows = paymentBillMapper.queryForList(qo);

        return new PageResult(rows, total);
    }

    /**
     * 缴费功能实现
     *
     * @param id        当前缴费人员的id
     * @param begindate 缴费人员的起保时间
     * @param payWayId  缴费的支付方式
     */
    public void pay(Long id, Date begindate, Long payWayId) {
        //查询当前的缴费人员的具体信息
        PaymentBill paymentBill = paymentBillMapper.selectByPrimaryKey(id);

        //把缴费人员的具体信息移植到缴费成功的信息中
        //begin
        PaymentResult paymentResult = new PaymentResult();
        paymentResult.setLatentClient(paymentBill.getLatentClient());
        paymentResult.setNumber(paymentBill.getNumber());
        paymentResult.setPrice(paymentBill.getPrice());
        paymentResult.setLimityear(paymentBill.getLimityear());
        paymentResult.getDictionaryDetail().setId(payWayId);
        paymentResult.setBegindate(begindate);
        //计算止保日期
        Calendar cal = Calendar.getInstance();
        cal.setTime(begindate);
        cal.add(Calendar.YEAR, paymentBill.getLimityear());
        paymentResult.setEnddate(cal.getTime());
        paymentResult.getAgencyInfo().setId(paymentBill.getAgencyInfo().getId());
        //获取当前登录人的id
        Employee employee = (Employee) SecurityUtils.getSubject().getPrincipal();
        paymentResult.getEmployee().setId(employee.getId());
        paymentResultMapper.insert(paymentResult);
        //end

        String number = paymentBill.getNumber();
        //修改状态,改为已缴费的状态
        guaranteebillMapper.updateByNumber(number);


        SaleAccount sale = saleAccountMapper.selectByAllId(paymentBill);
        if (sale != null) {
            Long amount = sale.getAmount();
            Long price = Long.valueOf(paymentBill.getPrice());
            sale.setAmount(amount + price);
            saleAccountMapper.updateByPrimaryKey(sale);
        } else {
            //设置销售数据(报表表)
            SaleAccount saleAccount = new SaleAccount();
            saleAccount.setAmount(Long.valueOf(paymentBill.getPrice()));
            //saleAccount.getEmployee().setId(employee.getId());
            saleAccount.getAgencyInfo().setId(paymentBill.getAgencyInfo().getId());
            saleAccount.setProductId(paymentBill.getAgencyInfo().getId());
            Date date = new Date();
            saleAccount.setBilldate(date);
            saleAccountMapper.insert(saleAccount);
        }
        //删除数据
        paymentBillMapper.deleteByPrimaryKey(id);

    }

}
